﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EYDIngenieria.ERP.Aplicacion.Contabilidad ;
using EYDIngenieria.ERP.ObjetosDeNegocio.Dominio;

public partial class Contabilidad_IngresoVoucher : System.Web.UI.Page
{
    private EYDIngenieria.ERP.Aplicacion.Contabilidad.IngresoVoucher mantenedor = new IngresoVoucher();
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if(!IsPostBack)
            MantenedorVistaInicial();
       
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SetearFormulario();
        MantenedorVisualizacion();
    }

    private void SetearFormulario()
    {
        int id = (gdvVoucher.SelectedValue == null) ? 0 : int.Parse(gdvVoucher.SelectedValue.ToString());
        txbVoucher.Text = string.Empty;
        txbFecha.Text = string.Empty;
        txbDebe.Text = string.Empty;
        txbHaber.Text = string.Empty;
        txbGlosa.Text = string.Empty;
        
        if(id>0)
        {
            Voucher u = mantenedor.Obtener(id);
            txbVoucher.Text = u.IdVoucher.ToString();
            txbFecha.Text = u.Fecha.ToString();
            txbDebe.Text = u.Debe.ToString();
            txbHaber.Text = u.Haber.ToString();
            ddlCuenta.SelectedValue = u.Cuenta.Id.ToString();
            txbGlosa.Text = u.Glosa.ToString();
        }
        
    }

    private void MantenedorVistaInicial()
    {
        txbVoucher.Enabled = false;
        txbFecha.Enabled = false;
        txbDebe.Enabled = false;
        txbHaber.Enabled = false;
        ddlCuenta.Enabled = false;
        txbGlosa.Enabled = false;
        btnNuevo.Visible = true;
        btnAceptar.Visible = false;
        btnCancelar.Visible = false;
        btnEliminar.Visible = false;
        btnModificar.Visible = false;
        SetearFormulario();
    }

    private void MantenedorVisualizacion()
    {
        SetearFormulario();
        txbVoucher.Enabled = false;
        txbFecha.Enabled = false;
        txbDebe.Enabled = false;
        txbHaber.Enabled = false;
        txbGlosa.Enabled = false;
        ddlCuenta.Enabled = false;
        btnNuevo.Visible = true;
        btnAceptar.Visible = false;
        btnCancelar.Visible = false;
        btnEliminar.Visible = true;
        btnModificar.Visible = true;
        
    }

    private void MantenedorVistaGestion()
    {
        SetearFormulario();
        txbVoucher.Enabled = true;
        txbFecha.Enabled = true;
        txbDebe.Enabled = true;
        txbHaber.Enabled = true;
        txbGlosa.Enabled = true;
        ddlCuenta.Enabled = true;
        btnNuevo.Visible = false;
        btnAceptar.Visible = true;
        btnCancelar.Visible = true;
        btnEliminar.Visible = false;
        btnModificar.Visible = false;
    }
    
    protected void btnNuevo_Click(object sender, EventArgs e)
    {
        gdvVoucher.SelectedIndex = -1;
        SetearFormulario();
        MantenedorVistaGestion();
    }
    protected void btnModificar_Click(object sender, EventArgs e)
    {      
        MantenedorVistaGestion();
    }
    protected void btnEliminar_Click(object sender, EventArgs e)
    {
        int id = (gdvVoucher.SelectedValue == null) ? 0 : int.Parse(gdvVoucher.SelectedValue.ToString());
        if(id ==0) 
            return;

        Voucher u = mantenedor.Obtener(id);
        u.IdVoucher = int.Parse(txbVoucher.Text);
        u.Fecha = DateTime.Parse(txbFecha.Text);
        u.Debe = int.Parse(txbDebe.Text);
        u.Haber = int.Parse(txbHaber.Text);
        u.Glosa = txbGlosa.Text;
        mantenedor.Eliminar(u);
        gdvVoucher.SelectedIndex = -1;
        ActualizaDataSource();
        SetearFormulario();
    }
    protected void btnCancelar_Click(object sender, EventArgs e)
    {
        int id = (gdvVoucher.SelectedValue == null) ? 0 : int.Parse(gdvVoucher.SelectedValue.ToString());
        if(id>0)
        MantenedorVisualizacion();
        else
            MantenedorVistaInicial();
    }
    protected void btnAceptar_Click(object sender, EventArgs e)
    {
        int id = (gdvVoucher.SelectedValue == null) ? 0 : int.Parse(gdvVoucher.SelectedValue.ToString());
        EYDIngenieria.ERP.ObjetosDeNegocio.Dominio.Voucher o = new Voucher();
        o.IdVoucher = int.Parse(txbVoucher.Text);
        o.Fecha = DateTime.Parse(txbFecha.Text);
        o.Id = id;
        o.Debe = int.Parse(txbDebe.Text);
        o.Haber = int.Parse(txbHaber.Text);
        o.Glosa = txbGlosa.Text;
        o.Cuenta = new PlanDeCuenta();
        o.Cuenta.Id = int.Parse(ddlCuenta.SelectedValue);
                                                
        if(id==0) 
            mantenedor.Agregar(o);
        else
            mantenedor.Modificar(o);
       
        ActualizaDataSource();
        MantenedorVistaInicial();
       
    }

    private void ActualizaDataSource()
    {
        odsVoucher.Select();
        odsVoucher.DataBind();
        gdvVoucher.DataBind();

        odsVoucher.Select();
        odsVoucher.DataBind();
    }
    
}
